package com.dlc.commonlibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.dlc.commonlibrary.R;
import com.dlc.commonlibrary.utils.DialogUtil;

public class EditDialog extends Dialog implements View.OnClickListener {

    TextView mTvTitle;
    EditText mEtContent;
    Button mBtnNeg;
    Button mBtnPos;

    private Context mContext;

    private OnPositiveClickListener mOnPositiveClickListener;
    private String mOldText = "";
    private TextView mTvCount;

    public EditDialog(@NonNull Context context) {
        super(context, R.style.CommonDialogStyle);
        mContext = context;
        initDialog();
    }

    public static EditDialog newDialog(Context context) {
        return new EditDialog(context);
    }

    private void initDialog() {

        setContentView(R.layout.dialog_edit);
        DialogUtil.adjustDialogLayout(this, true, false);

        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mEtContent = (EditText) findViewById(R.id.et_content);
        mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mTvCount.setText(s.length() + "/200");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBtnNeg = (Button) findViewById(R.id.btn_neg);
        mBtnNeg.setOnClickListener(this);
        mBtnPos = (Button) findViewById(R.id.btn_pos);
        mBtnPos.setOnClickListener(this);

        mTvCount = (TextView) findViewById(R.id.tv_count);

        DialogUtil.setAutoShowIme(this);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_neg) {
            dismiss();
        } else if (i == R.id.btn_pos) {
            if (mOnPositiveClickListener != null) {
                mOnPositiveClickListener.onPositiveClick(this, mEtContent.getText().toString(),
                    mOldText);
            }
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mEtContent.setText(null);
    }

    public interface OnPositiveClickListener {
        /**
         * @param dialog
         * @param newText
         * @param oldText
         */
        void onPositiveClick(EditDialog dialog, @NonNull String newText, @NonNull String oldText);
    }

    public EditDialog setOnPositiveClickListener(OnPositiveClickListener onPositiveClickListener) {
        mOnPositiveClickListener = onPositiveClickListener;
        return this;
    }

    public EditDialog setTitle(String title) {
        mTvTitle.setText(title);
        return this;
    }

    public EditDialog setTitleRes(int stringResId) {
        mTvTitle.setText(stringResId);
        return this;
    }

    public EditDialog setContent(String content) {
        if (content != null) {
            mOldText = content;
        }
        mEtContent.setText(mOldText);
        mEtContent.setSelection(mEtContent.length());
        return this;
    }

    public EditDialog setContent(int stringResId) {
        mOldText = getContext().getResources().getString(stringResId);
        mEtContent.setText(mOldText);
        mEtContent.setSelection(mEtContent.length());
        return this;
    }

    public EditDialog setHint(String hint) {
        mEtContent.setHint(hint);
        return this;
    }

    public EditDialog setHint(int stringResId) {
        mEtContent.setHint(stringResId);
        return this;
    }

    public EditDialog setButton(int positive, int negative) {
        mBtnNeg.setText(negative);
        mBtnPos.setText(positive);
        return this;
    }

    public EditDialog setButton(String positive, String negative) {
        mBtnNeg.setText(negative);
        mBtnPos.setText(positive);
        return this;
    }
}
